package com.cvit.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cvit.model.Msg;
import com.cvit.model.Time;
import com.cvit.service.TimeService;
import com.cvit.utils.Utils;

@Controller
public class TimeController {
	@Autowired
	TimeService timeService;
	/**
	 * 填报志愿的查询
	 * */
	@ResponseBody
	@RequestMapping("/getVolunteer")
	public Msg getVolunteer(){
		List<Time> volunteer=timeService.getVolunteer();
		return Msg.success().add("volunteer", volunteer);
	}
	/**
	 * 打印准考证的查询
	 * */
	@ResponseBody
	@RequestMapping("/getPrint")
	public Msg getPrint(){
		List<Time> print=timeService.getPrint();
		return Msg.success().add("print", print);
	}
	/**
	 * 添加
	 * */
	@ResponseBody
	@RequestMapping("/addTime")
	public Msg addTime(Time time){
		timeService.addTime(time);
		return Msg.success();
	}
	/**
	 * 修改
	 * @throws ParseException 
	 * */
	@ResponseBody
	@RequestMapping("/updateTime")
	public Msg updateTime(@RequestParam("id")String id,@RequestParam("starlTime")String starlTime,@RequestParam("endTime")String endTime,@RequestParam("timeType")Integer timeType) throws ParseException{
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
		Time times = new Time();
		times.setId(id);
		times.setEndTime(df.parse(endTime));
		times.setStarlTime(df.parse(starlTime));
		times.setTimeType(timeType);
		timeService.updateTimes(times);
		return Msg.success();
		
	}
	
	/**
	 * @author Ws
	 * 根据Id查询
	 * */
	@ResponseBody
	@RequestMapping("/getTimeById")
	public Msg getTimeById(@RequestParam("id")String id){
		Time time=timeService.getTimeById(id);
		return Msg.success().add("Time", time);
	}  
	
	/**
	 * 查询是否可以打印准考证
	 * */
	@ResponseBody
	@RequestMapping("/getTime2")
	public Msg getTime2(){
		String timeId="2";
		Time time = timeService.getTimeById(timeId);//获取可用时间段
		int code = Utils.isTime(time.getStarlTime(), time.getEndTime());
		if(code == 0){
			return Msg.success();
		}else if(code == 1){
			return Msg.fail().add("code", "未到打印时间");
		}else
			return Msg.fail().add("code", "打印时间已结束");
		
	}
	
	
}
